package stream;

import org.junit.BeforeClass;
import org.junit.Test;

import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 正则测试
 * Created by Robo on 2018/7/20.
 */
public class RegexTest {
    String content = "I am noob from runoob.com.";
    String pattern = ".*runoob.*";
    @BeforeClass
    public static void initList(){
        System.out.println("init...");
    }

    @Test
    public void testFirst(){
//        boolean isMatch = Pattern.matches(pattern, content);
        boolean isMatch = pattern.matches(content);
        System.out.println("字符串中是否包含了 'runoob' 子字符串? " + isMatch);
        System.out.println((int)(Math.floor(888 * Math.random())));//获取随机小于888的整数
        System.out.println(888%221);
        String ts = "a.3";
        String[] split = ts.split("\\.");
        for (String s : split) {
            System.out.println(s);
        }

    }

    @Test
    public void testNumFormat(){
        int youNumber = 65846;
        String STR_FORMAT = "000000"; // 默认6为流水号
        DecimalFormat df = new DecimalFormat(STR_FORMAT);
        String s = df.format(youNumber);
        System.out.println(s);

        String str = String.format("%06d", youNumber);
        System.out.println(str);
//        str = String.format("%-6d", youNumber);
        str = String.format("%-9s", youNumber).replace(' ','0');
        System.out.println(str);

        //年度取后两位
        System.out.println(youNumber%100);

//        StringBuffer buffer = df.format(123, new StringBuffer(4), new FieldPosition(4));
//        System.out.println(buffer.toString());
    }
    @Test
    public void testObj() {
        Object a = new Object();
        Object b = new Object();
//        System.out.println(a==b);
//        System.out.println(a.equals(b));
        new Object().equals("");
        int i = 2;
        Integer i2 = 2;
        Integer i3 = Integer.valueOf(2);
        Integer i4 = new Integer(2);//只要是重新new的就一定是新建的对象
        System.out.println(i==i2);
        System.out.println(i3==i2);
        System.out.println(i4==i2);
        System.out.println(i4==i3);
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        RegexTest regexTest = (RegexTest) o;

        if (content != null ? !content.equals(regexTest.content) : regexTest.content != null) return false;
        return pattern != null ? pattern.equals(regexTest.pattern) : regexTest.pattern == null;
    }

    @Override
    public int hashCode() {
        int result = content != null ? content.hashCode() : 0;
        result = 31 * result + (pattern != null ? pattern.hashCode() : 0);
        return result;
    }

    @Test
    public void tesSql(){
        String KEY_WORD = "$";
        Pattern pattern = Pattern.compile("(?i)\\" + KEY_WORD + "(\\w+\\.?\\w+)\\b\\" + KEY_WORD);
        String sql = "select id ID, fjdxid PARENTID, bm as CODE,mc as TEXT, bm||'-'||mc CODETEXT" +
                " from $bmysjjkm$ where deleted=0 and id = 1 order by bm";

        Matcher matcher = pattern.matcher(sql);
        if (matcher.find()) {
            System.out.println("groupCount : " + matcher.groupCount() + ", match group : " + matcher.group(1) +
                        " start " + matcher.start() + " end " + matcher.end());
            String matchTable = matcher.group(1);
            String realText = realSql(matchTable);
            sql =  sql.replace(KEY_WORD + matchTable + KEY_WORD, realText);
            System.out.println("字符串中是否包含了  子字符串 realText: " + realText + " \nsql: " + sql);
        }
    }

    public String realSql(String tableName) {
        return "(select * from " + tableName + "xx where nd = 2018 )";
    }

    @Test
    public void findNumber(){
        // 按指定模式在字符串查找
        String line = "This order was placed for QT3000! OK?";
        String pattern = "(\\D*)(\\d+)(.*)";

        // 创建 Pattern 对象
        Pattern r = Pattern.compile(pattern);

        // 现在创建 matcher 对象
        Matcher m = r.matcher(line);
        if (m.find( )) {
            System.out.println("Found value: " + m.group(0) );//全部
            System.out.println("Found value: " + m.group(1) );//数字前边
            System.out.println("Found value: " + m.group(2) );//只有数字
            System.out.println("Found value: " + m.group(3) );//数字后边
        } else {
            System.out.println("NO MATCH");
        }
    }
}
